<?php
/**
 * 查看自己的团队
 */
apf_require_controller("Abstract");
apf_require_class("Bll_Jx_Relation");
apf_require_class("Bll_Jx_Setting");
apf_require_class("Bll_Jx_Plan");

class Grade_ShowViewDepartmentController extends AbstractController {

    public function handle_request() {
        $params = $this->request->get_parameters();
        $user_id = empty($params['user_id']) ? $this->request->get_user_id() : intval($params['user_id']);

        $is_lower =  empty($params['is_lower']) ? 0 : intval($params['is_lower']);
        $setting_bll = new Bll_Jx_Setting();
        $rel_bll = new Bll_Jx_Relation();
        $plan_bll = new Bll_Jx_Plan();
        $last_month = $setting_bll->format_performance();

        $view_user = $rel_bll->get_user_info($user_id);
        if(empty($view_user)){
            $this->show_tips('ERROR_PARAM');
        }
        $relation = $rel_bll->get_has_sub_user_list($view_user['code']);
        $is_show_team = array();
        //被查看人不是主管
        if(empty($relation[$view_user['code']])){
            $this->show_tips('ERROR_PARAM');
        }
        //不参加考评的人
        $no_kp_users = $rel_bll->get_kp_user(1);

        //这里有免考，不能过滤，会影响
        $sub_codes = array();
        foreach($relation[$view_user['code']] as $val){
            $sub_codes[$val['code']] = $val['user_id'];
        }
        $codes = array_keys($sub_codes);
        $is_show_team = $rel_bll->check_has_sub_user($codes);
        //所有下属还没有下属

        $code_to_uid = $rel_bll->get_code_to_uid();
        $under_team = $sub_team = $lower_team = array();
        $datalist = array();

        if($is_lower || empty($is_show_team)){//查看直属团队的,这里不过滤免考，只是在前台显示会标识
            foreach($relation[$view_user['code']] as $val){
                $info = $plan_bll->get_one_performance($val['user_id'],$last_month[0]);
                $info['score'] = empty($info['adjustScore']) ? $info['personScore'] : $info['adjustScore'];
                $info['name'] = $val['name'];
                $info['is_no_kp'] = $val['flag'];//TODO如果是查看历史的数据,就不知道他是不是免考了
                $lower_team[$val['user_id']] =$info;
            }
        }else{
            //如果code=>1说明这个员工下面还有下属，如果是code=0的真在直属团队
            foreach ($is_show_team as $code=>$val){
                $uid = $code_to_uid[$code]['user_id'];
                if(!$uid){
                    continue;
                }
                if(!$val){//有直属团队,直接取绩效详情
                    $under_team[$code] = $plan_bll->get_one_performance($uid,$last_month[0]);
                }else{
                    $sub_team[$code] = $plan_bll->get_sub_user_kp($code,$last_month[0]);
                }
            }
        }

        if(!empty($sub_team)){
            foreach($sub_team as $code=> $sub_list){
                $uid = $code_to_uid[$code]['user_id'];
                $datalist[$uid] = $code_to_uid[$code];
                $datalist[$uid]['max_users'] = count($sub_list) ;
                foreach ($sub_list as $val){
                    //要过滤免考了
                    if(!empty($no_kp_users[$val['userId']])){
                        continue;
                    }
                    $score = empty($val['adjustScore']) ? $val['personScore'] : $val['adjustScore'];
                    $str = Const_JxStatus::check_score_stage($score);
                    $datalist[$uid][$str] +=1;
                    $datalist[$uid]['status'] = $val['status'];//为了前台显示通知他调整
                }
            }
        }
        //print_r($sub_team);
        $under_team_list = array();
        if(!empty($under_team)){
            $datalist['under']['max_users'] = count($under_team);
            foreach ($under_team as $val){
                //要过滤免考了
                if(!empty($no_kp_users[$val['userId']])){
                    continue;
                }

                $score = empty($val['adjustScore']) ? $val['personScore'] : $val['adjustScore'];
                $str = Const_JxStatus::check_score_stage($score);
                $datalist['under']['user_id'] =$view_user['user_id'];
                $datalist['under']['name'] =$view_user['name'].'直属';
                $datalist['under'][$str] +=1;
                $under_team_list[$str] +=1;
            }
        }
        //print_r($under_team_list);
        //根据当前用户所有上级,第一个271提交者
        $this->request->set_attribute('user_name','271提交者');
//print_r($is_show_team);
        $this->request->set_attribute('lower_team',$lower_team);
        $this->request->set_attribute('datalist',$datalist);
        $this->request->set_attribute('under_team_list',$under_team_list);

        return 'Grade_ShowViewDepartment';
    }

}